function findLoans($scope, CertusFormAjax) {
    // 结果页-人员借调申请记录

    var colistColumns = [{
        id: 'operaterName',
        title: '申请人',
        type: 'string',
        width: 75,
        columnClass: 'text-center',
    }, {
        id: 'createTime',
        title: '申请时间',
        type: 'date',
        width: 110,
        format: 'yyyy-MM-dd',
        otype: 'time_stamp_ms',
        columnClass: 'text-center'
    }, {
        id: 'loanComments',
        title: '说明',
        type: 'string',
        columnClass: 'text-center',
    },{
        id: 'proName',
        title: '归属项目',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'nodeFlag',
        title: '状态',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value) {
            var content = '';
            if (value=="1"){
                content='申请待审批';
            }else if(value=="2"){
                content='拒绝';
            }else if(value=="3"){
                content='已退回';
            }else if (value=="4"){
                content='待审批';
            }else if (value=="5"){
                content='待区域经理审批';
            }else{
                content='申请通过';
            }
            return content;
        }
    },{
        id: 'operation',
        title: '操作',
        width: 70,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            content += '<a class="btn btn-xs btn-primary" onclick="detail(\''+record.processInstanceId+'\')"><i class="icon-eye-open"></i>  查看</a>';
            return content;
        }
    }];
    CertusFormAjax.ajax('findLoanList',{},function (response) {
        var colistOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '人员借调申请记录',
            datas: response.data,
            columns: colistColumns,
            gridContainer: 'colistContainer',
            toolbarContainer: 'colistToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        var colistGrid = $.fn.dlshouwen.grid.init(colistOption);
        colistGrid.load();
    });
};

function detail(id) {
    window.location.href='#/selloc/'+id;
}

function selcmp() {
  // layer.alert(
  // '<select class="form-control" id="selcmp">'+
  //   '<option value="HFKJ">浩方科技</option>'+
  //   '<option value="STSBJ">赛特斯北京</option>'+
  //   '<option value="STSGD">赛特斯广东</option>'+
  //   '<option value="STSJT">赛特斯集团</option>'+
  //   '<option value="STSNJ">赛特斯南京</option>'+
  //   '<option value="STSSH">赛特斯上海</option>'+
  //   '<option value="SYB-RX">融合运维事业部</option>'+
  // '</select>', {
  //   skin: 'layer-ext-moon',
  //   title: '请选择人力输出方的公司/事业部'
  // }, function (index) {
  //   layer.close(index);
  //   var cmpVal = $("#selcmp").val();
  //   var cmpText = $("#selcmp option:selected").text();
  //   window.location.href = '#/addloc';
  //   layer.msg('你选择了：' + cmpText + ' 对应编码：' + cmpVal);
  // })
    window.location.href = '#/addloc';
}

function selout() {
  layer.alert(
    '<input type="text" class="form-control" id="selout" placeholder="项目名称">'
  , {
    skin: 'layer-ext-moon',
    title: '请输入需要释放已借调人员的项目'
  }, function (index) {
    layer.close(index);
    var cmpVal = $("#selout").val();
    // var cmpText = $("#selout option:selected").text();
    window.location.href = '#/addlocout';
    layer.msg('你选择了项目：' + cmpVal);
  })
}

// 展示隐藏
function coladd() {
  var status = $("#addloc").css('display');
  if (status === "block") {
    $("#addloc").hide()
  } else {
    $("#addloc").show()
  }
}

// 审批页-人员借调列表
var userlisinfoColumns = [{
    id: 'cmpName',
    title: '公司',
    type: 'string',
    columnClass: 'text-center',
  }, {
    id: 'depName',
    title: '借调部门',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
  }, {
    id: 'proName',
    title: '归属项目',
    type: 'string',
    columnClass: 'text-center',
  },
  {
    id: 'contNo',
    title: '合同编号',
    type: 'string',
    columnClass: 'text-center',
  }, {
    id: 'userType',
    title: '人员类别',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
  }, {
    id: 'userName',
    title: '人员姓名',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
  }, {
    id: 'time',
    title: '工作周期',
    type: 'string',
    width: 200,
    columnClass: 'text-center',
  }, {
    id: 'accf',
    title: '投入占比',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      var content = value + '%';
      return content;
  }
  }, {
    id: 'work',
    title: '工作任务',
    type: 'string',
    columnClass: 'text-left',
  }, {
    id: 'operation',
    title: '操作',
    width: 70,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      var content = '';
      content += '<button class="btn btn-xs btn-success" onclick="coladd()"><i class="fa fa-edit"></i>  编辑</button>';
      // content += '  ';
      // content += '<button class="btn btn-xs btn-danger" onclick=""><i class="fa fa-trash-o"></i>  删除</button>';
      return content;
    }
  }
];
var userlisinfoOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '人员借调列表',
  datas: [{
    cmpName: '赛特斯集团',
    depName: '赛特斯XX部门>赛特斯XX部门>赛特斯XX部门',
    proName: '项目A',
    contNo: 'NJ201934922',
    userType: '客服',
    userName: '张三',
    time: '2018-01-02至2019-01-01',
    accf: '15%',
    work: '工作内容测试数据'
  }],
  columns: userlisinfoColumns,
  gridContainer: 'userlisinfoContainer',
  toolbarContainer: 'userlisinfoToolBarContainer',
  tools: '',
  pageSize: 100,
  pageSizeLimit: [10, 15, 20]
};
var userlisinfoGrid = $.fn.dlshouwen.grid.init(userlisinfoOption);
$(function () {
  userlisinfoGrid.load();
});



// 结果页-人员借调列表
var userlastColumns = [{
    id: 'cmpName',
    title: '公司',
    type: 'string',
    columnClass: 'text-center',
  }, {
    id: 'depName',
    title: '借调部门',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
  }, {
    id: 'proName',
    title: '归属项目',
    type: 'string',
    columnClass: 'text-center',
  },
  {
    id: 'contNo',
    title: '合同编号',
    type: 'string',
    columnClass: 'text-center',
  }, {
    id: 'userType',
    title: '人员类别',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
  }, {
    id: 'userName',
    title: '人员姓名',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
  }, {
    id: 'time',
    title: '工作周期',
    type: 'string',
    width: 200,
    columnClass: 'text-center',
  }, {
    id: 'accf',
    title: '投入占比',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      var content = value + '%';
      return content;
  }
  }, {
    id: 'work',
    title: '工作任务',
    type: 'string',
    columnClass: 'text-left',
  }
];
var userlastOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '人员借调列表',
  datas: [{
    cmpName: '赛特斯集团',
    depName: '赛特斯XX部门>赛特斯XX部门>赛特斯XX部门',
    proName: '项目A',
    contNo: 'NJ201934922',
    userType: '客服',
    userName: '张三',
    time: '2018-01-02至2019-01-01',
    accf: '15%',
    work: '工作内容测试数据'
  }],
  columns: userlastColumns,
  gridContainer: 'userlastContainer',
  toolbarContainer: 'userlastToolBarContainer',
  tools: '',
  pageSize: 100,
  pageSizeLimit: [10, 15, 20]
};
var userlastGrid = $.fn.dlshouwen.grid.init(userlastOption);
$(function () {
  userlastGrid.load();
});



// 项目结果页-人员调用清单
var coloutColumns = [{
  id: 'cmpName',
  title: '公司',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'depName',
  title: '原部门(借调前)',
  type: 'string',
  columnClass: 'text-center',
}, {
  id: 'userType',
  title: '人员类别',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'userName',
  title: '人员姓名',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, ];
var coloutOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '人员调用清单',
  datas: [{
    cmpName: '赛特斯集团',
    depName: '赛特斯XX部门>赛特斯XX部门>赛特斯XX部门',
    userType: '客服',
    userName: '张三',
  }],
  check: true,
  checkWidth: 20,
  columns: coloutColumns,
  gridContainer: 'coloutContainer',
  toolbarContainer: 'coloutToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var coloutGrid = $.fn.dlshouwen.grid.init(coloutOption);
$(function () {
  coloutGrid.load();
});

// 人员调出清单
var outlisColumns = [{
  id: 'cmpName',
  title: '公司',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'depName',
  title: '原部门(借调前)',
  type: 'string',
  columnClass: 'text-center',
}, {
  id: 'userType',
  title: '人员类别',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'userName',
  title: '人员姓名',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, ];
var outlisOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '人员调出列表',
  datas: [{
    cmpName: '赛特斯集团',
    depName: '赛特斯XX部门>赛特斯XX部门>赛特斯XX部门',
    userType: '客服',
    userName: '张三',
  }],
  columns: outlisColumns,
  gridContainer: 'outlisContainer',
  toolbarContainer: 'outlisToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var outlisGrid = $.fn.dlshouwen.grid.init(outlisOption);
$(function () {
  outlisGrid.load();
});


function addLoc($scope, CertusFormAjax) {

    this.getProContract = function () {
        CertusFormAjax.ajax("contractProgresss",{"projId":$("#proId").val()},function (response) {
            var data = response.data;
            $("#contractId").html("");
            $("#contractId").append('<option value="">请选择</option>');
            for (var i=0;i<data.length;i++){
                var ctinfo = data[i].contractInfo;
                $("#contractId").append("<option value='"+ctinfo.contractNo+"'>"+ctinfo.contractNo+"</option>")
            }
        });
    };

    this.getUser = function (compCode) {
        $("#dyUserName").val("");
        $("#dyUserId").val("");
        $("#deptCode").val("");
        if (compCode==""){
            $("#dyUserName").attr("disabled",true);
            return;
        }
        $("#dyUserName").attr("disabled",false);
        // 开启
        var index = layer.load(2);
        CertusFormAjax.ajax("findUsersByComp",{"compCode":compCode},function (response) {
            var nhlTeams = [];
            var data = response.data;
            for (var i=0;i<data.length;i++){
                var arr={};
                arr.name=data[i].userName;
                arr.id = data[i].userId;
                nhlTeams.push(arr);
            }
            //自动补全
            var nhl = $.map(nhlTeams, function (obj) {
                return {
                    value: obj.name+"("+obj.id+")",
                    data: { category: obj.id }
                };
            });
            // Initialize autocomplete with local lookup:
            $('#dyUserName').devbridgeAutocomplete({
                lookup: nhl,
                minChars: 1,
                onSelect: function (suggestion) {
                    $("#dyUserId").val(suggestion.data.category);
                    getDep_1();
                },
                showNoSuggestionNotice: true,
                noSuggestionNotice: 'Sorry, no matching results'
            });
            //关闭
            layer.close(index);
        });

        CertusFormAjax.ajax("findProListByCompId",{"compId":compCode},function(response){
            var nhlTeams = [];
            var data = response.data;
            for (var i=0;i<data.length;i++){
                var arr={};
                arr.name=data[i].proName;
                arr.id = data[i].id;
                nhlTeams.push(arr);
            }
            //自动补全
            var nhl = $.map(nhlTeams, function (obj) {
                return {
                    value: obj.name,
                    data: { category: obj.id }
                };
            });
            // Initialize autocomplete with local lookup:
            $('#proName').devbridgeAutocomplete({
                lookup: nhl,
                minChars: 1,
                onSelect: function (suggestion) {
                    $("#proId").val(suggestion.data.category);
                    getProContract();
                },
                showNoSuggestionNotice: true,
                noSuggestionNotice: 'Sorry, no matching results'
            });
        });
    };

    //所属公司
    // CertusFormAjax.ajax("companyListByViewCode",{},function (response) {
    //     $scope.comp = JSON.parse(response.data);
    // });

    this.getDep_1 = function () {
        CertusFormAjax.ajax('initDepts',{"userId":$("#dyUserId").val()},function (response) {
            var data = response.data;
            $scope.depts = data;
            // if (data.length==1){
            //     $("#deptCode").attr("disabled","disabled");
            // }
            // $scope.deptCode = data[0].departmentCode;
        });
    };

    // if (!certusValidateNotnull()){
    //     return;
    // }
    //人员调用列表
    // 申请页-人员借调列表
    var userlistColumns = [{
        id: 'deptName',
        title: '借调部门',
        type: 'string',
        width: 200,
        columnClass: 'text-center'
    }, {
            id: 'dyType',
            title: '人员类别',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'dyUserName',
            title: '人员姓名',
            type: 'string',
            width: 75,
            columnClass: 'text-center'
        }, {
            id: 'workStart',
            title: '开始日期',
            type: 'string',
            width: 100,
            columnClass: 'text-center'
        }, {
            id: 'workEnd',
            title: '结束日期',
            type: 'string',
            width: 100,
            columnClass: 'text-center'
        },{
            id: 'invoiceSl',
            title: '投入占比',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
              var content = value + '%';
              return content;
          }
        }, {
            id: 'comments',
            title: '工作任务',
            type: 'string',
            columnClass: 'text-left',
        }, {
            id: 'operation',
            title: '操作',
            width: 60,
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '';
                // content += '<button class="btn btn-xs btn-success" onclick="coladd()"><i class="fa fa-edit"></i>  编辑</button>';
                // content += '  ';
                content += '<button class="btn btn-xs btn-danger" onclick="delArr('+record.id+')"><i class="fa fa-trash-o"></i>  删除</button>';
                return content;
            }
        }
    ];
    this.loadlb = function (paramArr) {
      $("#userlistContainer").empty();
      var userlistOption = {
          lang: 'zh-cn',
          ajaxLoad: false,
          // loadURL:'url',
          exportFileName: '人员借调列表',
          datas: paramArr,
          columns: userlistColumns,
          gridContainer: 'userlistContainer',
          tools: ''
      };
      var userlistGrid = $.fn.dlshouwen.grid.init(userlistOption);
      userlistGrid.load();
    };
    var userArr = [];
    loadlb(userArr);

    this.clearTab = function () {
        $("#dyType").val("kf");
        $("#dyUserId").val("");
        $("#dyUserName").val("");
        $("#deptCode").val("");
        $("#workStart").val("");
        $("#workEnd").val("");
        $("#comments").val("");
        $("#invoiceSl").val("");
    };

    var nid=1;
    $(".tj").on('click',function () {
        var dyType = $("#dyType").val();
        var dyUserId = $("#dyUserId").val();
        if (dyUserId==""){
            layer.msg("借调人员不能为空");
            return;
        }
        var deptCode = $("#deptCode").val();
        if (deptCode==""){
            layer.msg("借调人员归属部门不能为空");
            return;
        }
        var workStart = $("#workStart").val();
        if (workStart==""){
            layer.msg("开始日期不能为空");
            return;
        }
        var workEnd = $("#workEnd").val();
        if (workEnd==""){
            layer.msg("结束日期不能为空");
            return;
        }
        var beginTimes = new Date(workStart.replace("/", "-").replace("/", "-"));
        var endTimes = new Date(workEnd.replace("/", "-").replace("/", "-"));
        if (beginTimes>endTimes){
            layer.msg("开始日期不能大于结束日期",{time:1000});
            return;
        }
        var comments = $("#comments").val();
        if (comments.trim()==""){
            layer.msg("工作任务不能为空");
            return;
        }
        var invoiceSl = $("#invoiceSl").val();
        if (invoiceSl==""){
            layer.msg("投入百分比不能为空");
            return;
        }

        var arr = {};
        arr.dyType = dyType;
        arr.dyUserId = dyUserId;
        arr.dyUserName = $("#dyUserName").val();
        arr.deptCode = deptCode;
        arr.deptName = $('#deptCode option:selected').text();
        arr.workStart = workStart;
        arr.workEnd = workEnd;
        arr.comments = comments;
        arr.invoiceSl = invoiceSl;
        arr.id = nid;
        userArr.push(arr);
        nid++;
        loadlb(userArr);
        clearTab();
    });

    //删除
    this.delArr = function (id) {
      layer.confirm('是否确认删除？', {
        btn: ['确定', '取消'] //按钮
      }, function () {
        var arr ={};
        for (var i=0;i<userArr.length;i++){
            if (userArr[i].id == id){
                arr = userArr[i];
            }
        }
        userArr.splice($.inArray(arr, userArr), 1);
        layer.msg("删除成功");
        loadlb(userArr);
      });
    };

    //提交申请
    $(".sumbit").on('click',function () {
        if (userArr.length==0){
            layer.msg("至少提交一个借调人员");
            return;
        }
        var companyId = $("#companys").val();
        if (companyId==""){
            layer.msg("公司不能为空");
            return;
        }
        var companyName = $('#companys option:selected').text();
        var proId = $("#proId").val();
        if (proId==""){
            layer.msg("归属项目不能为空");
            return;
        }
        var proName = $("#proName").val();
        var contractId = $("#contractId").val();
        // if (contractId==""){
        //     layer.msg("合同不能为空");
        //     return;
        // }
        var loanComments = $("#loanComments").val();
        var arr ={};
        arr.companyId = companyId;
        arr.companyName = companyName;
        arr.proId = proId;
        arr.proName = proName;
        arr.contractId = contractId;
        arr.loanComments = loanComments;
        arr.userList = userArr;
        CertusFormAjax.ajax('addLoan',{"param":JSON.stringify(arr)},function () {
            layer.msg("申请成功");
            window.history.back();
        });
    });
}